package guis.classroom;

import exceptions.RepositoryException;
import guis.ComponentFactory;
import guis.ModalWindow;
import guis.RegisterPanel;

import java.awt.event.KeyEvent;
import java.util.Vector;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JTextField;

import classes.Classroom;
import classes.Member;

import communication.Facade;

public class ClassroomWindow extends ModalWindow
{
	private static final long				serialVersionUID	= 1032307379765488755L;
	protected JTextField					nameTextField;
	protected JTextField					magazineTextFild;
	protected JComboBox						titularComboBox;
	protected JComboBox						auxiliarComboBox;
	protected JButton						actionButton;
	protected JButton						cancelButton;
	protected RegisterPanel< Classroom >	registerPanel;
	protected Facade						facade;

	public ClassroomWindow( String title, String actionButtonFunction, JFrame source, RegisterPanel< Classroom > registerPanel, Facade facade )
	{
		super( title, source );
		this.facade = facade;
		this.registerPanel = registerPanel;
		initialize();
		createLabels();
		createTextFields();
		createComboBoxes();
		createButtons( actionButtonFunction );
	}

	private void initialize()
	{
		this.setSize( 490, 160 );
		this.setLocationRelativeTo( null );
	}

	private void createLabels()
	{
		ComponentFactory.createLabel( "Nome:", 10, 10, getContentPane() );
		ComponentFactory.createLabel( "Revista:", 10, 40, getContentPane() );
		ComponentFactory.createLabel( "Titular", 10, 70, getContentPane() );
		ComponentFactory.createLabel( "Auxiliar:", 10, 100, getContentPane() );
	}

	private void createTextFields()
	{
		this.nameTextField = ComponentFactory.createJTextField( 250, 22, 100, 10, getContentPane() );
		this.magazineTextFild = ComponentFactory.createJTextField( 250, 22, 100, 40, getContentPane() );
	}

	private void createComboBoxes()
	{
		Vector< Member > members = new Vector< Member >();
		try
		{
			members = facade.getIteratorMember().toVector();
			members.add( 0, null );
		}
		catch ( RepositoryException e )
		{
		}
		this.titularComboBox = ComponentFactory.createJComboBox( members, 250, 22, 100, 70, getContentPane() );
		this.auxiliarComboBox = ComponentFactory.createJComboBox( members, 250, 22, 100, 100, getContentPane() );
	}

	private void createButtons( String actionButtonFunction )
	{
		this.actionButton = ComponentFactory.createJButton( actionButtonFunction, true, 110, 22, 365, 70, null, getContentPane() );
		this.cancelButton = ComponentFactory.createJButton( "cancelar", true, 110, 22, 365, 100, null, getContentPane() );

		if ( actionButtonFunction.equals( "inserir" ) )
			actionButton.setMnemonic( KeyEvent.VK_I );
		else
			actionButton.setMnemonic( KeyEvent.VK_B );
		cancelButton.setMnemonic( KeyEvent.VK_C );
	}

	public void setVisible( boolean arg0 )
	{
		super.setVisible( arg0 );
		if ( !arg0 )
		{
			source.setEnabled( true );
			source.setVisible( true );
		}
	}
}
